package fun.mashuai.copy;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.io.file.FileCopier;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

/**
 * @author 马帅
 * @version 1.0
 * @description TODO
 * @className Copy.java
 * @date 2022/3/23 16:53
 */
public class Copy {

    private static String src = "D:\\dev-center\\map-data\\BingMap\\BingMap_SatelliteNone_World_zxy_1-14\\12";
    //private static String dest = "\\\\172.17.100.22\\share\\bing_zxy\\11";
    private static String dest = "D:\\dev-center\\map-data\\BingMap\\12";

    private static Integer xianchen = 16;


    private static class CopyFile implements Runnable{
        private List<File> list;
        private String destPath;

        public CopyFile(List<File> list, String destPath){
            this.list = list;
            this.destPath = destPath;
        }

        @Override
        public void run() {
            long startTime = System.currentTimeMillis();

            InputStream in = null;
            OutputStream out = null;

            StreamProgressObj streamProgressObj = null;

            try {

                out = new FileOutputStream(destPath);

                streamProgressObj = new StreamProgressObj();

                for(File file : list){
                    System.out.println(file);
                    FileUtil.copy(file.getPath(), destPath, false);

                    in = new FileInputStream(file);
                    IoUtil.copy(in, out,1024, streamProgressObj);
                }

            } catch (IOException e) {
                //抛出一个运行时异常(直接停止掉程序)
                throw new RuntimeException("运行时异常",e);
            } finally {
                IoUtil.closeIfPosible(out);
                IoUtil.closeIfPosible(in);
            }


            long endTime = System.currentTimeMillis();
            System.out.println("程序运行时间：" + (endTime - startTime)/1000 + "秒");

        }
    }

    public static class StreamProgressObj implements StreamProgress {
        @Override
        public void start() {
            System.out.println("copy操作进度开始");
        }

        @Override
        public void progress(long progressSize) {
            System.out.println("当前copy操作进度："+progressSize);
        }

        @Override
        public void finish() {
            System.out.println("copy操作进度结束");
        }
    }

    public static void main(String[] args) {
        int cpuNum = Runtime.getRuntime().availableProcessors();
        System.out.println(cpuNum);

        // 线程数 4 * 100% * 100% / 50% = 8
        //int xianchen = cpuNum * 80% * 80% / 50%;


        if(args != null){
            if(args.length >= 1){
                src = args[0];
                dest = args[1];
                System.out.println("源目录：" + src);
                System.out.println("目标目录：" + dest);
            }
            if(args.length >= 2){
                xianchen = Integer.valueOf(args[2]);
            }

        } else {
            System.out.println("请输入2个参数：源目录与目标目录，空格隔开");
        }


        // 获取源目录中所有文件绝对路径
        //List<File> list = FileUtil.loopFiles(src);
        List<File> list = Arrays.asList(FileUtil.ls(src)).subList(0,20);

        int size = list.size();
        System.out.println("文件路径：" + list);
        System.out.println("文件数量：" + size);

        // 平均分成20份
        List<List<File>> lists1 = ListUtil.splitAvg(list, 5);
        System.out.println(lists1);

        for(List<File> list1 : lists1){
            CopyFile copyFile = new CopyFile(list1, dest);
            new Thread(copyFile).start();

        }





    }
}
